/*
 * Copyright (C) 2010 Rodion Volkov (volkov.rodion@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rchik.divermate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;

public class LoadDonation extends Activity {

	private boolean loadingView;
	
	@Override
	public void onContentChanged() {
		
		super.onContentChanged();
		
		if ( loadingView )
		{
			loadingView = false;
			
			HttpClient httpclient = new DefaultHttpClient();  
		    HttpParams myParams = httpclient.getParams();
		    HttpConnectionParams.setConnectionTimeout(myParams,40000);
	        HttpConnectionParams.setSoTimeout(myParams,40000);
		    HttpPost httppost = new HttpPost("https://www.paypal.com/cgi-bin/webscr");  
		  
		    try {  
		        // Add your data  
		        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
		        nameValuePairs.add(new BasicNameValuePair("cmd", "_s-xclick"));  
		        nameValuePairs.add(new BasicNameValuePair("hosted_button_id", "NRZRF2RWJT494"));
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
		  
		        // Execute HTTP Post Request  
		        HttpResponse response = httpclient.execute(httppost);  
		        
		        InputStream is = response.getEntity().getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	            StringBuilder str = new StringBuilder();
	            String line = null;
	            while((line = reader.readLine()) != null){
	                str.append(line + "\n");
	            }
	            is.close();
		        String responseText = str.toString();
		        WebView webview = new WebView(this);
		        webview.getSettings().setBuiltInZoomControls(true);
		        MyWebViewClient wvclient = new MyWebViewClient();
		        wvclient.setActivity(this);
		        webview.setWebViewClient(wvclient);
		        webview.loadDataWithBaseURL("https://www.paypal.com/cgi-bin/webscr", responseText, "text/html", "utf-8", "http://code.google.com/p/divermate"); 
		          
		    } 
		    catch (ClientProtocolException e) 
		    {  
		    	Log.w("CPE", e.getMessage());
		    } 
		    catch (IOException e) 
		    {  
		        ((TextView)findViewById(R.id.LoadingTextView)).setText(R.string.load_failed);
		    }
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		loadingView = true;
		
		setContentView(R.layout.loading_donation); 
	}

}
